package view;

import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.AbstractCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JTree;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;

//import model.FolderBean;

/**
 * Class that manages user edit actions (basically, the clicks on the checkbox)
 * 
 * For rendering and editing data we use the same component (JCheckBox) so we're
 * going to reuse CheckboxCellRenderer_1 class to configure it, we just need to
 * add a listener for the event triggered when the user changes the state of the
 * component.
 */
public class CheckboxNodeEditor_1 extends AbstractCellEditor implements TreeCellEditor {

 //   private TreeCellRenderer renderer = new CheckBoxNodeRenderer();
 private CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();

    private JCheckBox editor2;
    private boolean firstCall = true;
   // JTree tree;


  // public CheckboxNodeEditor_1(JTree tree) {
  //  this.tree = tree;
 // }
     @Override
   public JCheckBox getTreeCellEditorComponent(JTree tree, Object value,
      boolean selected, boolean expanded, boolean leaf, int row) {

    JCheckBox editor = (JCheckBox) renderer.getTreeCellRendererComponent(tree, value,
        true, expanded, leaf, row, true);

    // editor always selected / focused
    ItemListener itemListener = new ItemListener() {
      public void itemStateChanged(ItemEvent itemEvent) {
        if (stopCellEditing()) {
          fireEditingStopped();
        }
      }
    };
    if (editor instanceof JCheckBox) {
      ((JCheckBox) editor).addItemListener(itemListener);
    }

    return editor;
  }

   
    @Override
   public Object getCellEditorValue() {
       JCheckBox checkbox = renderer.getLeafRenderer();
    CheckBoxNode checkBoxNode = new CheckBoxNode(checkbox.getText(),
        checkbox.isSelected());
    return checkBoxNode;
    }

}
